package com.dalin.system.service.imple;

import com.dalin.framework.BaseServiceImple;
import com.dalin.framework.dao.ConnectFactory;
import com.dalin.framework.dao.DbConn;
import com.dalin.framework.vos.JdbcVo;
import com.dalin.framework.vos.PageInfoVo;
import com.dalin.framework.vos.ResResult;
import com.dalin.system.dao.DbColumnMapper;
import com.dalin.system.dao.${entityName}Mapper;
import com.dalin.system.dao.DbTableMapper;
import com.dalin.system.pojo.entity.DbColumnEntity;
import com.dalin.system.pojo.entity.${entityName}Entity;
import com.dalin.system.pojo.entity.DbTableEntity;
import com.dalin.system.pojo.request.${entityName}Request;
import com.dalin.system.pojo.request.DbTableReq;
import com.dalin.system.pojo.response.${entityName}Vo;
import com.dalin.system.service.${entityName}Service;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ${entityName}ServiceImpl extends BaseServiceImple implements ${entityName}Service {
    @Autowired
    private ${entityName}Mapper ${lowerEntityName}Mapper;

    @Override
    public ResResult listPage${entityName}s(${entityName}Request req) {
        log.info("listPage${entityName}s params:"+req.getPageNum() + "," + req.getPageSize());
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List l = ${lowerEntityName}Mapper.list${entityName}s(req);
        PageInfoVo piv = new PageInfoVo(l);
        piv.setList(get${entityName}Vos(l));
        return ResResult.getSuccessRes(piv);
    }
    public List<${entityName}Vo> get${entityName}Vos(List<${entityName}Entity> orderInfoList) {
        return orderInfoList.stream().map(entity -> {
            ${entityName}Vo vo = new ${entityName}Vo();
            BeanUtils.copyProperties(entity, vo);
            vo.setId(entity.getId()+"");
            //vo.setDbTypeDisplay(entity.getDbTypeDisplay());
            return vo;
        }).collect(Collectors.toList());
    }

    public String addOrUpdate${entityName}(${entityName}Entity ${lowerEntityName}) {
        if (${entityName}.getId() == null || ${entityName}.getId() == 0) {
            ${lowerEntityName}Mapper.insert(${entityName});
        } else {
            ${lowerEntityName}Mapper.updateById(${entityName});
        }
        return ${entityName}.getId();
    }



    public String delete${entityName}(String id) {
        int i = ${lowerEntityName}Mapper.deleteById(id);
        System.out.println("${lowerEntityName}Mapper.deleteById("+id+")return ===="+i);
        return id;
    }

    public void delete${entityName}s(List<String> ids) {
        int i = ${lowerEntityName}Mapper.deleteBatchIds(ids);
       // return id;
    }

    public ${entityName}Entity get${entityName}ById(int id) {
        return ${lowerEntityName}Mapper.selectById(id);
    }
}